import os
import re
import pdfplumber
import xlwt

# 获取当前文件夹中所有的PDF文件
HANDLE_PATH = '.'
pdf_files = [f for f in os.listdir(HANDLE_PATH) if os.path.isfile(f) and f.endswith('.pdf')]
path = ''
paths = "./转换之后/"
# 检查路径是否存在，如果不存在则创建
if not os.path.exists(paths):
    os.makedirs(paths)

for pdf_file in pdf_files:
    # 检查是否已经存在转换过的Excel文件，如果存在则跳过
    excel_file = pdf_file.replace('.pdf', '.xls')
    if os.path.exists(excel_file):
        continue

    # 打开PDF文件
    with pdfplumber.open(pdf_file) as pdf:
        num_pages = len(pdf.pages)
        text = ''
        parts_info = []
        for page in range(num_pages):
            pdf_page = pdf.pages[page]
            text += pdf_page.extract_text()
            delivery_notice_number_search = re.search(r'Forwarding No. Supply point - Bay\s+([\w\d]+)', text, re.DOTALL)
            if delivery_notice_number_search is not None:
                delivery_notice_number = delivery_notice_number_search.group(1)
            else:
                print("No delivery notice number found in text.")
                delivery_notice_number = None
            lines = text.split('\n')
            parts_info = []
            for line in lines:
                match = re.match(r'(\d+)\s+([\w\d]+)\s+([\w\s/-]+)\s+(\d{8})\s+PCE\s+(\d+)\s+(\d+)', line)
                if match:
                    parts_info.append(match.groups())
            print('parts_info', parts_info)
        workbook = xlwt.Workbook()
        worksheet = workbook.add_sheet('供应商供货清单')

        info = [
            ('发运编号：', '', '', '', '', '', '客户：', '马瑞利汽车零部件(芜湖)有限公司', '', '', ''),
            ('供应商', '', '', '', '', '', '采购订单编号：', '', '', '', ''),
            ('供应商地址：', '', '', '', '', '', '收货地址：', '', '', '', ''),
            ('序号', '零件号', '零件名称', '到货数量', '单位', '使用车型', '到货包装数', '标准包装数', '批次', '送货通知单号', '备注')
        ]

        font_style = xlwt.easyxf('font: height 280, bold on;')  # 设置字体大小为14，加粗
        borders = xlwt.Borders()  # 创建边框对象
        borders.left = xlwt.Borders.THIN
        borders.right = xlwt.Borders.THIN
        borders.top = xlwt.Borders.THIN
        borders.bottom = xlwt.Borders.THIN
        font_style.borders = borders  # 将边框应用到字体样式

        for row, row_data in enumerate(info):
            for col, cell_data in enumerate(row_data):
                worksheet.write(row + 1, col, cell_data, font_style)

        for col in range(11):  # 设置所有列的宽度为15个字符宽度
            worksheet.col(col).width = 256 * 30

        title_style = xlwt.easyxf('align: horiz center, vert center; font: height 280, bold on;')
        title_style.borders = borders
        worksheet.write_merge(0, 0, 0, 10, '供应商供货清单（国内供应商）', title_style)

        # 设置数据行的字体样式和边框
        data_style = xlwt.easyxf('font: height 280;')
        data_style.borders = borders

        for index, part_info in enumerate(parts_info):
            worksheet.write(index + 5, 0, index + 1, data_style)
            worksheet.write(index + 5, 1, part_info[1], data_style)  # 零件号
            worksheet.write(index + 5, 2, part_info[2], data_style)  # 零件名称
            worksheet.write(index + 5, 3, part_info[4], data_style)  # 到货数量
            worksheet.write(index + 5, 4, '', data_style)  # 单位
            worksheet.write(index + 5, 5, '', data_style)  # 使用车型
            worksheet.write(index + 5, 6, '', data_style)  # 到货包装数
            worksheet.write(index + 5, 7, '', data_style)  # 标准包装数
            worksheet.write(index + 5, 8, part_info[3], data_style)  # 批次
            worksheet.write(index + 5, 9, delivery_notice_number, data_style)
            worksheet.write(index + 5, 10, '', data_style)  # 备注

        # 保存Excel文件
        workbook.save(paths + excel_file)
